package com.hblack.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hblack.common.bean.EasyUIResult;
import com.hblack.manage.pojo.Item;
import com.hblack.manage.service.ItemService;

@Controller
@RequestMapping(value="item")
public class ItemController {
	
	@Autowired
	private ItemService itemService;

	/**
	 * 保存商品
	 * @param item
	 * @param desc
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> saveItem(Item item,@RequestParam("desc") String desc, @RequestParam("itemParams") String itemParams){
		
		try {
			Boolean saveItem = itemService.saveItem(item, desc, itemParams);
			if(saveItem){
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	/**
	 * 分页查询商品
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryPage(@RequestParam(value="page",defaultValue="1")Integer pageNum, @RequestParam(value="rows",defaultValue="30")Integer pageSize){
		
		try {
			EasyUIResult result = itemService.queryPage(pageNum, pageSize);
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateItemByItemId(Item item,@RequestParam("desc")String itemDesc,@RequestParam("itemParams")String params,@RequestParam("itemParamId") Long itemParamId){
		
		try {
			Boolean updateItem = this.itemService.updateItemByItemId(item,itemDesc,params,itemParamId);
			if(updateItem){
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		
	}
}
